/*
 * @lc app=leetcode.cn id=206 lang=javascript
 *
 * [206] 反转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
  //双指针法
  //preNode指向前一个节点
  //curNode指向当前节点
  //每次让 curnode.next -> preNode 反装
  //用temp临时存储
  let preNode = null;
  let curNode = head;
  let temp = null;
  while (curNode) {
    temp = curNode.next;
    curNode.next = preNode;
    preNode = curNode;
    curNode = temp;
  }
  return preNode ? preNode : curNode;
};
/* var reverseList = function(head) {
    if(head == null || head.next == null){
        return head;
    }
    const nodeList = reverseList(head.next);
    head.next.next =head
    head.next = null
    return nodeList
    }; */
// @lc code=end
